<?php
/* @var $this UsersController */
/* @var $model Users */
/* @var $form CActiveForm */
?>
<style>
    #Users_gender label {
        margin-left: 20px;
    }
</style>

<div class="col-md-12 col-sm-12 col-xs-12">
    <?php $form = $this->beginWidget('CActiveForm', array(
            'id'            => 'users-form',
            'enableClientValidation' => true,
            'enableAjaxValidation'   => true,
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
            'htmlOptions'   => array(
                'enctype' => 'multipart/form-data',
                'class'   => 'form-horizontal',
            )
        )
    ); ?>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'fullname', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->textField($model, 'fullname', array('class' => 'form-control')); ?>
        </div>
        <?php echo $form->error($model, 'fullname'); ?>
    </div>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'username', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->textField($model, 'username', array('class' => 'form-control')); ?>
        </div>
        <?php echo $form->error($model, 'username'); ?>
    </div>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'email', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->textField($model, 'email', array('class' => 'form-control')); ?>
        </div>
        <?php echo $form->error($model, 'email'); ?>
    </div>
    <?php if ($model->isNewRecord) { ?>
        <div class="form-group">
            <?php echo $form->labelEx($model, 'password', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
            <div class="col-md-6 col-sm-6">
                <?php echo $form->passwordField($model, 'password', array('class' => 'form-control')); ?>
            </div>
            <?php echo $form->error($model, 'password'); ?>
        </div>
        <div class="form-group">
            <?php echo $form->labelEx($model, 'confirmPassword', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
            <div class="col-md-6 col-sm-6">
                <?php echo $form->passwordField($model, 'confirmPassword', array('class' => 'form-control')); ?>
            </div>
            <?php echo $form->error($model, 'confirmPassword'); ?>
        </div>
    <?php } ?>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'phone', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->textField($model, 'phone', array('class' => 'form-control')); ?>
        </div>
        <?php echo $form->error($model, 'phone'); ?>
    </div>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'avatar', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->hiddenField($model, "avatar"); ?>
            <?php $this->widget('CMultiFileUpload', array(
                    'name'      => 'avatar',
                    'accept'    => 'jpeg|jpg|gif|png', // useful for verifying files
                    'duplicate' => 'Duplicate file!', // useful, i think
                    'denied'    => 'Invalid file type',
                    // useful, i think
                ));
            echo $form->error($model, 'avatar');

            if ($model->avatar):
                $image = explode(',', $model->avatar);
                foreach ($image as $k => $file):
                    ?>
                    <div class="clearfix"></div>
                    <span><?php echo $file; ?> &nbsp;
                <a href="javascript:;" onclick="javascript:removeFile('<?php echo $file; ?>', <?php echo $k; ?>)" title="Remove"
                   class="remove_<?php echo $k; ?>">Remove File</a></span>
                    <div class="clearfix"></div>
                <?php
                endforeach;
            endif;?>
        </div>
    </div>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'permission', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->dropDownList($model, 'permission', Lookup::items('ROLE')); ?>
        </div>
        <?php echo $form->error($model, 'permission'); ?>
    </div>
    <div class="form-group">
        <?php echo $form->labelEx($model, 'gender', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->dropDownList($model, 'gender', Lookup::items('GENDER')); ?>
        </div>
        <?php echo $form->error($model, 'gender'); ?>
    </div>
    <!--<div class="form-group">
        <div class="col-sm-9 col-md-offset-3">
            <input type="datetime" class="form-control hidden" name="txtNgayTao">
        </div>
    </div>-->
    <div class="form-group">
        <?php echo $form->labelEx($model, 'status', array('class' => 'col-md-3 col-sm-3 control-label')); ?>
        <div class="col-md-6 col-sm-6">
            <?php echo $form->dropDownList($model, 'status', Lookup::items('STATUS')); ?>
        </div>
    </div>
    <div class="col-sm-6 col-sm-offset-3">
        <div class="pull-right">
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Tạo mới' : 'Sửa', array('class' => 'btn btn-primary m-b-10')); ?>
            <?php echo CHtml::resetButton('Nhập lại', array('class' => 'btn btn-default m-b-10')); ?>
        </div>
    </div>
    <?php $this->endWidget(); ?>
</div>

<?php
$scriptDelete = '
if("' . $model->avatar . '" != ""){
    function removeFile(fileName, pos) {
        var url = "' . Yii::app()->controller->createUrl('/backend/default/deleteimage') . '";
        var imageID = "' . $model->id . '";
        var colImage = "avatar";

        $.post(
            url, { imageID: imageID, colImage: colImage, imageName: fileName, model: "Users" },
            function(data){
                $("a.remove_"+pos).parent().remove();
                parent.window.location = parent.window.location
            }, "json"
        );

        return false;
    }
}
';
Yii::app()->clientScript->registerScript('scriptDelete', $scriptDelete, CClientScript::POS_END);